package GUI.Ventas.Facturacion;

import java.rmi.RemoteException;

import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

import GUI.Administracion.Exceptions.ExcepcionesMenuVentas;
import sale.entities.FacturaVO;
import sale.exceptions.EmptySaleExcpetion;
import service.ServiceFacade;
import service.article.exceptions.ProxyException;
import service.sale.managers.SaleProxyMgr;

public class SetDescuento extends Dialog {

	protected Object result;
	protected Shell shell;
	private Text text;
	Label error;
	private Button btnCancelar;
	FacturaVO fac;
	/**
	 * Create the dialog.
	 * @param parent
	 * @param style
	 */
	public SetDescuento(Shell parent, int style, FacturaVO fact) {
		super(parent, style);
		setText("SWT Dialog");
		fac=fact;
	}

	/**
	 * Open the dialog.
	 * @return the result
	 */
	public Object open() {
		createContents();
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	/**
	 * Create contents of the dialog.
	 */
	private void createContents() {
		shell = new Shell(getParent(), getStyle());
		shell.setSize(549, 145);
		shell.setText(getText());
		shell.setLayout(null);
		
		Label lblPorcentajeDeDescuento = new Label(shell, SWT.NONE);
		lblPorcentajeDeDescuento.setBounds(14, 26, 140, 14);
		lblPorcentajeDeDescuento.setText("Porcentaje de Descuento ");
		
		text = new Text(shell, SWT.BORDER);
		text.setBounds(168, 24, 176, 19);
		
		error = new Label(shell, SWT.NONE);
		error.setBounds(14, 48, 330, 14);
		error.setText("");
		
		btnCancelar = new Button(shell, SWT.NONE);
		btnCancelar.setBounds(331, 72, 94, 28);
		btnCancelar.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				shell.dispose();
			}
		});
		btnCancelar.setText("Cancelar");
		
		Button btnAceptar = new Button(shell, SWT.NONE);
		btnAceptar.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				int d=0;
				System.out.println("entre");
				if(text.getText().equalsIgnoreCase("")){
					error.setText("ingrese un porcentaje de descuento");
				}
				else{
				d = Integer.parseInt(text.getText());
				System.out.println("entre" + d);
				}
				
				boolean er=false;
				if(d>100){
					error.setText("el porcentaje de descuento debe estar entre 0 y 100");
					er=true;
				}
				if(d<=0){
					error.setText("el porcentaje de descuento debe estar entre 0 y 100");
					er=true;
				}
				if(er==false){
					fac.setDescuento(d);
				
					System.out.println("seteado");
					CerrarVenta c = new CerrarVenta (shell, shell.getStyle(), null, fac);
					try {
						c.open();
						System.out.println("open");
						shell.dispose();
					} catch (RemoteException e1) {
						ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
						ventana.open();
						shell.dispose();
						e1.printStackTrace();
					} catch (ProxyException e1) {
						ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
						ventana.open();
						shell.dispose();
						e1.printStackTrace();
					} catch (EmptySaleExcpetion e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					
				}
			}
			
		});
		btnAceptar.setBounds(185, 72, 94, 28);
		btnAceptar.setText("Aceptar");

	}
}
